package api

import (
	"django_blog_gin/internal/model"
	"django_blog_gin/internal/response"
	"django_blog_gin/internal/svc"
	"fmt"

	"github.com/gin-gonic/gin"
)

type GetAuthGroupList struct {
	svcCtx *svc.ServiceContext
}

func NewGetAuthGroupList(svcCtx *svc.ServiceContext) gin.HandlerFunc {
	return GetAuthGroupList{svcCtx: svcCtx}.getAuthGroupList
}

func (i GetAuthGroupList) getAuthGroupList(c *gin.Context) {
	authGroup := []model.AuthGroup{}
	sql := fmt.Sprintf("SELECT * FROM %s", model.AuthGroup{}.TableName())
	err := i.svcCtx.MysqlDb.Select(&authGroup, sql)
	if err != nil {
		c.JSON(response.CodeFail, gin.H{"error": err.Error()})
		return
	}
	c.JSON(response.CodeSuccess, gin.H{"data": authGroup})
}
